package gitee.android.KUI.widget

import android.annotation.SuppressLint
import android.app.ActionBar.LayoutParams
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Point
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import gitee.android.KUI.MeasureText
import gitee.android.KUI.R
import gitee.android.KUI.dp

/**
 * @author 程序员Keven
 * 时间 : 2023-01-08
 * 描述 : 微信索引栏
 */


interface iIndexBar{
    fun earnSelectText(text : String);
}
class IndexBar @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    View(context, attrs) {
    private var text = "IndexBar";

    /**
     * 索引栏文字大小
     */
    private var textSize: Float = 15.dp;

    /**
     * 索引栏的文字颜色
     */
    private var textColor: Int = Color.BLACK;

    /**
     * 索引栏文字上部间距
     */
    private var distance: Float = 3.dp;


    // 当前选中的原点的 x坐标
    private var selectCenterX = -10f;
    // 当前选中的原点的 y坐标
    private var selectCenterY = -10f ;
    //半径
    private var r = 14
    // 所欲文字 中心点的集合
    private var centerDots = mutableListOf<Point>()

    // 选中的文字

    private var SelectText = "";

    /**
     * 描述文字的样式
     */

    private  var DescStype  = 0;

    /**
     * 描述和文字的间距
     */
    private var textFromDesc = 20;

    /**
     * 索引栏的文字颜色
     */
    private var indexBarTextColor = Color.BLACK;

    /**
     * 索引栏的描述区域大小
     */
    private var descOutLineSize : Int = 0 ;

    /**
     *索引栏选中的颜色
     */

    private var indexBarSelectBackgroundColor = Color.GREEN;


    /**
     * 描述区域文字的大小
     */
    private var descTextSize = 10f;
    /**
     * 描述区域的文字颜色
     */

    private var descTextColor = Color.WHITE;

    /**
     *描述区域的背景颜色
     */

    private var descOutLineBackGroundColor = Color.GRAY;



    private lateinit var outerCanvas : Canvas;


    /**
     * 索引栏文字的画笔
     */
    private  lateinit var textPaint  :Paint;

    /**
     * 索引栏文字的背景画笔
     */
    private lateinit var  bgPaint : Paint;

    /**
     * 描述区域背景画笔
     */
    private lateinit var  descBgPaint : Paint;


    /**
     * 描述区域文字画笔
     */
    private lateinit var descTextPaint : Paint;


    private lateinit var iIndexBar : iIndexBar;

    init {


        val style =
            context!!.obtainStyledAttributes(attrs, R.styleable.IndexBar);

        distance = style.getDimension(R.styleable.IndexBar_distance, 5f)
        textSize = style.getDimension(R.styleable.IndexBar_indexBar_text_size, 15f)
        text = style.getString(R.styleable.IndexBar_indexBar_text).toString();

        indexBarTextColor  = style.getColor(R.styleable.IndexBar_index_bar_text_color , Color.BLACK);

        indexBarSelectBackgroundColor = style.getColor(R.styleable.IndexBar_index_bar_select_bgcolor , Color.GREEN);

        descOutLineSize = style.getDimension(R.styleable.IndexBar_desc_outline_size , 50f).toInt();

        descTextSize = style.getDimension(R.styleable.IndexBar_desc_text_size , 10f).toFloat();

        descTextColor = style.getColor(R.styleable.IndexBar_desc_text_color , Color.WHITE);


        textFromDesc = style.getDimension(R.styleable.IndexBar_desc_from_text_distance ,20f).toInt();

        descOutLineBackGroundColor = style.getColor(R.styleable.IndexBar_desc_outline_bg_color , Color.GRAY);


        DescStype = style.getInteger(R.styleable.IndexBar_desc_style , -1);
        if (DescStype == 0){
            descOutLineSize = 50
        }else if (DescStype == 1){
            descOutLineSize = 50;
        }
        style.recycle();

        requestLayout();
        initPaint()
    }

    private fun initPaint() {

        textPaint = Paint();
        textPaint.textSize = textSize;
        textPaint.color = textColor;
        textPaint.isAntiAlias = true;
        textPaint.strokeWidth = 1f;


        bgPaint = Paint();
        bgPaint.textSize = textSize;
        bgPaint.color = indexBarSelectBackgroundColor;
        bgPaint.isAntiAlias = true;
        bgPaint.strokeWidth = 1f;


        descBgPaint = Paint();
        descBgPaint.textSize = 1f;
        descBgPaint.color =descOutLineBackGroundColor
        descBgPaint.isAntiAlias = true;
        descBgPaint.strokeWidth = 1f;


        descTextPaint = Paint();
        descTextPaint.color = descTextColor;
        descTextPaint.textSize = descTextSize;
        descBgPaint.isAntiAlias = true;
        descBgPaint.strokeWidth = 1f;
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)


        var makeWithMeasureSpec = widthMeasureSpec ;
        var makeHeightMeasureSpec =  heightMeasureSpec;
        // 1. 如果 指定大小  = > w h = 指定大小

         makeWithMeasureSpec = MeasureSpec.makeMeasureSpec(layoutParams.width , MeasureSpec.AT_MOST);

        //无论 时 w 还是指定高度   都是 字母加间距的高
         makeHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
            (MeasureText(text, textSize) + ((text.length - 1) * distance)).toInt(), MeasureSpec.UNSPECIFIED);

        // 2. 如果 宽 为 w w = 字体大小  如果 h = w h => 所有字符 和 间距 所占大小
        if (layoutParams.width == LayoutParams.WRAP_CONTENT) {
            makeWithMeasureSpec =
                MeasureSpec.makeMeasureSpec(textSize.toInt(), MeasureSpec.UNSPECIFIED);
        }



        if (layoutParams.width == LayoutParams.MATCH_PARENT){
            makeWithMeasureSpec = widthMeasureSpec ;
        }
        if (layoutParams.height == LayoutParams.MATCH_PARENT){
            makeHeightMeasureSpec = heightMeasureSpec;
        }

        val NewsHeight = MeasureSpec.getSize(makeHeightMeasureSpec)
        var NewsWith = MeasureSpec.getSize(makeWithMeasureSpec)

        NewsWith = NewsWith + paddingLeft + paddingRight
        setMeasuredDimension(NewsWith.toInt()+2*descOutLineSize+textFromDesc, NewsHeight.toInt(),);
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        outerCanvas = canvas;
        outerCanvas = canvas;
        outerCanvas.drawCircle(selectCenterX, selectCenterY ,(textSize - 3) ,bgPaint);

        var x = measuredWidth /  2 - 5 ;
        var startY = textSize  /  2 ;
        var index = 1 ;
        text.forEach {
            // 绘制索引栏
            canvas!!.drawText(it+"" , x.toFloat() , (index * distance) +( (index+1) * startY) , textPaint )

            centerDots.add(Point(x.toFloat().toInt() ,
                ((index * distance) +( (index+1) * startY)).toInt()
            ));

            index++;
        }


        if(DescStype  == 0){

            canvas!!.drawText(SelectText , selectCenterX.toFloat()-descOutLineSize , selectCenterY + textSize /2, descTextPaint )
        }else if (DescStype == 1){
            var clipReatF  = RectF();
            clipReatF.top =selectCenterY - descOutLineSize /2  ;
            clipReatF.bottom =selectCenterY+descOutLineSize.toFloat();
            clipReatF.right = selectCenterX-20
            clipReatF.left = selectCenterX - descOutLineSize ;

            //绘制描述区域背景

            canvas!!.drawCircle( selectCenterX-descOutLineSize / 2 - textFromDesc , selectCenterY , descOutLineSize.toFloat()  / 2 , descBgPaint)
            canvas!!.drawText(SelectText , selectCenterX - ((descOutLineSize) * 3/ 4)-textFromDesc  , selectCenterY + textSize /2, descTextPaint )
        }
//        Log.e(TAG, "SelectText: "+SelectText )
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {


        val y = event.y;
        var i = 0 ;
        text.forEach {
            if (y >= centerDots.get(i).y - textSize /2 && y <= centerDots.get(i).y  + textSize /2){
                SelectText = it.toString();
                selectCenterY = centerDots.get(i).y.toFloat()-8;
                selectCenterX = centerDots.get(i).x.toFloat()+6;
            }
            i++;
        }
        if (event.action == MotionEvent.ACTION_UP){
//            Log.e(TAG, "onTouchEvent: 手指抬起" )
            selectCenterX = (-Int.MAX_VALUE).toFloat() ;
            selectCenterY =  (-Int.MAX_VALUE).toFloat();
        }
        this.iIndexBar.earnSelectText(SelectText);
//        Log.e(TAG, "onTouchEvent: "+y )
        r  = 10;
        invalidate()
        requestLayout()

        return true;
    }





    fun GetSelectText(indexBar: iIndexBar){
        this.iIndexBar = indexBar;
    }




}